/*
 * BaseTag.java
 *
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the flyingsoft, Version 1.0 (the "License");
 *
 * Company:北京东方飞扬软件股份有限公司
 * 主页：http://www.flyingsoft.cn
 *
 */
package org.geek.common.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * <p>所有Tag标签的基类</p>
 * @author ninglong 2011-12-21下午04:52:35
 * @version 1.0
 */
public abstract class BaseTag extends TagSupport{

	private static final long serialVersionUID = 1894720045938252765L;
	private String parentWinId;
	private String ComponentId;
	private String winId;
	private ApplicationContext applicationContext;
	  
	/**
	 * <p>获取ApplicationContext对象从而得相应的bean</p>
	 * @author ninglong 2011-12-21下午04:52:52
	 * @version 1.0
	 * @return
	 */
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	  }

    public int doStartTag()throws JspException{
    	this.parentWinId = this.pageContext.getRequest().getParameter("parentWinId");
    	this.winId = this.pageContext.getRequest().getParameter("winId");

    	String taloneId = (String)this.pageContext.getAttribute("ComponentId");
    	if (taloneId == null) {
    		taloneId = System.currentTimeMillis()+"";
    		this.pageContext.setAttribute("ComponentId", taloneId);
    	}
    	this.ComponentId = taloneId;
    	applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.pageContext.getServletContext());
    	
    	init();
    	
    	StringBuffer buffer = new StringBuffer();
    	buffer.append("<script type='text/javascript'>																").append("\r\n");
    	genernateJavaScript(buffer);
    	buffer.append("</script>																					").append("\r\n");
    	genernateHtml(buffer);
    	write(this.pageContext, buffer);
    	return EVAL_BODY_INCLUDE;
   }

	public int doEndTag() throws JspException{
		return EVAL_PAGE;
	}
	
	public void release(){
	    super.release();
	    destory();
	    this.parentWinId = "";
	    this.ComponentId = "";
	    this.applicationContext = null;
	}
	  
	/**
	 * <p>得到父窗体Id</p>
	 * @author ninglong 2011-12-21下午04:56:24
	 * @version 1.0
	 * @return 得到父窗体Id
	 */
	protected String getParentWinId(){
	    return this.parentWinId;
	}
	
	/**
	 * <p>传入一个基础的组合Id，此方法在此Id的基础上加系统当前时间生成新Id</p>
	 * @author ninglong 2011-12-21下午04:56:45
	 * @version 1.0
	 * @param id 组件Id
	 * @return 返回新生成的组合Id
	 */
	protected String getId(String id){
	   return id + this.ComponentId;
	}

	private void write(PageContext pageContext, StringBuffer buffer)throws JspException{
	    JspWriter out = pageContext.getOut();
	    try {
	      out.write(buffer.toString());
	    } catch (IOException e) {
	      throw new JspException(e);
	    }
	}
	/**
	 * <p>Tag中所有JavaScript代码</p>
	 * @author ninglong 2011-12-21下午04:53:54
	 * @version 1.0
	 * @param buffer
	 */
	protected abstract void genernateJavaScript(StringBuffer buffer);
	/**
	 * <p>初始化方法（比如变量等）</p>
	 * @author ninglong 2011-12-21下午04:53:17
	 * @version 1.0
	 */
	protected abstract void init();

	/**
	 * <p>销毁相关变量</p>
	 * @author ninglong 2011-12-21下午04:53:41
	 * @version 1.0
	 */
	protected abstract void destory();


    /**
     * <p>Tag中所有Html代码</p>
     * @author ninglong 2011-12-21下午04:55:10
     * @version 1.0
     * @param buffer
     */
    protected abstract void genernateHtml(StringBuffer buffer);

	/**
	 * @return 主布局的panelId，用于统一调整tabPanel自适应
	 */
	public String getWinId() {
		return winId;
	}
}
